/**
 * 
 */

package com.pp.admin.main.client.tab;

import com.google.gwt.event.dom.client.ClickHandler;
import com.pp.admin.common.client.beans.User;
import com.pp.admin.main.client.common.BaseContentsPanel;
import com.pp.admin.main.client.common.CrudOnCreatePage;
import com.pp.admin.main.client.tab.objectpages.CompleteUser;

/**
 * @author pandurang
 */
class CreateUserPanel extends BaseContentsPanel {

	private CompleteUser user = null;

	private CrudOnCreatePage crudTop = null;

	private CrudOnCreatePage crudBottom = null;

	CreateUserPanel(UserPage parent) {

		this.parent = parent;
		user = new CompleteUser();
		init(false);
	}

	CreateUserPanel(UserPage parent, User userBean) {

		this.parent = parent;
		user = new CompleteUser(userBean);
		init(true);
	}

	public void init(boolean edit) {
		this.setWidth("100%");
		this.setHeight("100%");
		if (edit) {
			crudTop = new CrudOnCreatePage(true);
			crudBottom = new CrudOnCreatePage(true);
		} else {
			crudTop = new CrudOnCreatePage(false);
			crudBottom = new CrudOnCreatePage(false);
		}
		ClickHandler parentCancelHandler = ((UserPage) this.parent).new CancelHandler();
		ClickHandler saveHandler = null;
		if (edit) {
			saveHandler = ((UserPage) this.parent).new UpdateClickHandler();
		} else {
			saveHandler = ((UserPage) this.parent).new SaveClickHandler();
		}
		crudTop.addSaveClickHandler(saveHandler);
		crudBottom.addSaveClickHandler(saveHandler);
		crudTop.addCancelClickHandler(parentCancelHandler);
		crudBottom.addCancelClickHandler(parentCancelHandler);
		this.add(crudTop);

		this.add(user);
		this.add(crudBottom);

	}

	User getUser() {

		/**
		 * TODO: add check for dirty flag.
		 */

		return user.getUser();
	}

	void validate() {

		/**
		 * TODO add validation of all the fields.
		 */
	}
}
